//1.策略模式的定义掩饰---两种常见写法（策略类和策略对象）

//第一种：策略类写法
class Customer{
    constructor(kind) {
        this.kind = kind;
    }
    pay(amount) {
        return this.kind.pay(amount);
    }
};

//策略类
class Normal{
    pay(amount) {
        return amount;
    }
}
class Menber{
    pay(amount) {
        return amount*0.9;
    }
}
class Vip{
    pay(amount) {
        return amount*0.7;
    }
}
//演示
let c1 = new Customer(new Normal());
console.log(c1.pay(100));
c1.kind = new Menber();
console.log(c1.pay(100));
c1.kind = new Vip;
console.log(c1.pay(100));
console.log('----------------');



//第二种：策略对象写法
class Customer2{
    constructor() {
        this.kind = {
            normal:function (amount) {console.log(amount)
                typeof amount != Number && (amount = arguments[1]);
                return amount
            },
            menber: function(amount) {
                typeof amount != Number && (amount = arguments[1]);
                return amount*0.85
            },
            vip: function(amount){
                typeof amount != Number && (amount = arguments[1]);
                return amount*0.6
            },
        }
    }
    pay(type,amount) {
        // return this.kind[type](amount);
        return this.kind[type].apply(null,arguments)
    }
};
let c2 = new Customer2();
console.log(c2.pay('normal',100));
console.log(c2.pay('menber',100));
console.log(c2.pay('vip',100));

